package org.grayrabbit.cms.service.lucene.impl;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.grayrabbit.cms.service.lucene.KeywordsLuceneService;
import org.grayrabbit.cms.service.lucene.SearchLuceneService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component("indexTaskUpdate")
public class IndexTaskUpdate {
	private static final Logger log = LoggerFactory.getLogger(IndexTaskUpdate.class);
	private static int STATUS = 0;

	@Autowired
	@Qualifier("searchLuceneServiceImpl")
	private SearchLuceneService searchLuceneService;

	@Autowired
	@Qualifier("keywordsLuceneServiceImpl")
	private KeywordsLuceneService keywordsLuceneService;

	public void start(Date date) {
		Timer timer = new Timer();
		timer.schedule(new TimeUpdateTimerTask(), date, 60000 * 60 * 24);
	}

	public synchronized static void changeStatus(int status) {
		STATUS = status;
	}

	/**
	 * 当前索引状态 正常使用 0<br>
	 * 正在自动索引 1<br>
	 * 正在对全部索引 2<br>
	 * 正在对文章索引 3<br>
	 * 正在对静态索引4<br>
	 * @return
	 */
	public static int getStatus() {
		return STATUS;
	}

	public boolean createAll() {
		synchronized (this) {
			if (STATUS > 0) {
				return false;
			}
			else {
				new Thread() {
					public void run() {
						log.debug("开始建立全部索引.....");
						changeStatus(2);
						searchLuceneService.mergerIndex(searchLuceneService.createHtmlIndex(), searchLuceneService.createContentIndex());
						keywordsLuceneService.createIndex();
						changeStatus(0);
						log.debug("结束建立全部索引.....");
					}
				}.start();
			}

		}
		return true;
	}

	public boolean createKeywords() {
		synchronized (this) {
			if (STATUS > 0) {
				return false;
			}
			else {
				new Thread() {
					public void run() {
						log.debug("开始建立问题索引.....");
						changeStatus(5);
						keywordsLuceneService.createIndex();
						changeStatus(0);
						log.debug("结束建立问题索引.....");
					}
				}.start();
			}

		}
		return true;
	}

	public boolean createContent() {
		synchronized (this) {
			if (STATUS > 0) {
				return false;
			}
			else {
				new Thread() {
					public void run() {
						log.debug("开始建立文章索引.....");
						changeStatus(3);
						searchLuceneService.mergerIndex(false, searchLuceneService.createContentIndex());
						changeStatus(0);
						log.debug("结束建立文章索引.....");
					}
				}.start();
			}

		}
		return true;
	}

	public boolean createHtml() {
		synchronized (this) {
			if (STATUS > 0) {
				return false;
			}
			else {
				new Thread() {
					public void run() {
						log.debug("开始建立Html索引.....");
						changeStatus(4);
						searchLuceneService.mergerIndex(searchLuceneService.createHtmlIndex(), false);
						changeStatus(0);
						log.debug("结束建立Html索引.....");
					}
				}.start();
			}

		}
		return true;
	}


	public static String getMsg() {
		if (STATUS == 1) {
			return "正在自动更新索引";
		}
		else if (STATUS == 2) {
			return "正在更新全部索引";
		}
		else if (STATUS == 3) {
			return "正在更新文章索引";
		}
		else if (STATUS == 4) {
			return "正在更新静态索引";
		}
		else if (STATUS == 5) {
			return "正在更新关键字索引";
		}
		return "";
	}

	class TimeUpdateTimerTask extends TimerTask {
		public void run() {
			changeStatus(1);
			searchLuceneService.mergerIndex(searchLuceneService.createHtmlIndex(), searchLuceneService.createContentIndex());
			changeStatus(0);
		}
	}

}
